package com.example.anew.journey.mynewjourney.logic.registeredOrLogin;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.base.BasePasswordFreeLoginAndRegisteredActivity;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.FirstChangeMainActivity;
import com.example.anew.journey.mynewjourney.logic.getVersion.GetVersionActivity;
import com.example.anew.journey.mynewjourney.logic.getVersion.bean.GetVersionBean;
import com.example.anew.journey.mynewjourney.logic.login.LoginActivity;
import com.example.anew.journey.mynewjourney.logic.setName.SetNameActivity;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/3/5
 * @desc 登陆或注册页面
 */

public class RegisteredOrLoginActivity extends BasePasswordFreeLoginAndRegisteredActivity {



    protected void onClickRelease() {
        Intent intent =  new Intent(RegisteredOrLoginActivity.this,LoginActivity.class); //LoginActivity
       // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       startActivity(intent);
    }
    /**
     * @desc 根据获取到的手机号跳转到获取验证码页面
     * */
    @Override
    protected void toGetVersionCode(String trim) {
        if(TextUtils.isEmpty(trim))return;
        if(third_party_login_state != null){
            if(third_party_login_state.equals("1")){
                Intent intent =  new Intent(RegisteredOrLoginActivity.this,SetNameActivity.class);
                intent.putExtra("phone",trim);
                intent.putExtra("third_party_login_state",third_party_login_state);
                intent.putExtra("third_party_login_id",third_party_login_id);
                intent.putExtra("third_party_login_name",third_party_login_name);
                intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                startActivity(intent);
            }
        }else{

            NLogger.e("TAG","========678========"+MY_PAGE_INTEGRATAION_CODE);
            if(MY_PAGE_INTEGRATAION_CODE == 1){
                loadVersionCode(trim);
            }else{
                LoadVersion(trim);
            }

        }

    }

    private void loadVersionCode(final String trim) {
        String url = RBConstants.SERVER_URL+RBConstants.OTHER_GET_VERSION_CODE;
        OkHttpUtils
                .get()
                .url(url)
                .addParams("tel", trim)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                        NLogger.e("TAG","=====获取免密码登陆验证码====NetworkResponseDate============"+NetworkResponseDate);
                        final GetVersionBean getVersion =  gson.fromJson(NetworkResponseDate,GetVersionBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(getVersion.errorCode == 200) {
                                    Intent intent =  new Intent(RegisteredOrLoginActivity.this,GetVersionActivity.class);
                                    intent.putExtra("phone",trim);
                                    intent.putExtra("my_login_state",MY_PAGE_INTEGRATAION_CODE);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                                    startActivity(intent);
                                    Toast.makeText(RegisteredOrLoginActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();

                                }else{
//                                    Intent intent = new Intent(PasswordFreeLoginActivity.this, RegisteredOrLoginActivity.class);
//                                    mContext.startActivity(intent);
//                                    finish();
                                    if(getVersion.errorMessage.equals("手机号已注册！")){
                                        MyApplication.getmHandler().postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                Intent intent =  new Intent(RegisteredOrLoginActivity.this,LoginActivity.class); //LoginActivity
                                                startActivity(intent);
                                            }
                                        },800);
                                        Toast.makeText(RegisteredOrLoginActivity.this,"手机号已注册！为您跳转到登录页面",Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(RegisteredOrLoginActivity.this,getVersion.errorMessage,Toast.LENGTH_SHORT).show();
                                    }


                                }

                            }
                        });


                        return null;
                    }
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Toast.makeText(RegisteredOrLoginActivity.this,"验证码发送失败，请重新发送",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    private void LoadVersion(final String trim) {
        String url = RBConstants.SERVER_URL+RBConstants.REGISTERED_GET_VERSION_CODE;
        OkHttpUtils
                .get()
                .url(url)
                .addParams("tel", trim)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                        NLogger.e("TAG","=====获取免密码登陆验证码====NetworkResponseDate============"+NetworkResponseDate);
                        final GetVersionBean getVersion = gson.fromJson(NetworkResponseDate,GetVersionBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(getVersion.errorCode == 200){
                                    Intent intent =  new Intent(RegisteredOrLoginActivity.this,GetVersionActivity.class);
                                    intent.putExtra("phone",trim);
                                    intent.putExtra("my_login_state",MY_PAGE_INTEGRATAION_CODE);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                                    startActivity(intent);
                                    Toast.makeText(RegisteredOrLoginActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();
                                }else{
                                    if(getVersion.errorMessage.equals("手机号已注册！")){
                                        MyApplication.getmHandler().postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                Intent intent =  new Intent(RegisteredOrLoginActivity.this,LoginActivity.class); //LoginActivity
                                                startActivity(intent);
                                            }
                                        },800);
                                        Toast.makeText(RegisteredOrLoginActivity.this,"手机号已注册！为您跳转到登录页面",Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(RegisteredOrLoginActivity.this,getVersion.errorMessage,Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });

                        return null;
                    }
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(RegisteredOrLoginActivity.this,"验证码发送失败，请重新发送",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 初始化数据
     * */
    @Override
    protected void initDate() {
        if(MY_PAGE_INTEGRATAION_CODE ==1){
            displayTvAgreement();
        }else{
            hiddenTvAgreement();
        }
    }

    @Override
    protected void toMainPager() {
        Intent intent =  new Intent(RegisteredOrLoginActivity.this,FirstChangeMainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
         MY_PAGE_INTEGRATAION_CODE = intent.getIntExtra("my_page_integration_code",0);
        NLogger.e("TAG","========123========"+MY_PAGE_INTEGRATAION_CODE);
        if(MY_PAGE_INTEGRATAION_CODE ==1){
            displayTvAgreement();
        }else{
            hiddenTvAgreement();
        }
    }


    //    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            return true;
//        }
//        return false;
//    }
}
